package A1动态规划;

public class LC_516最长回文子序列 {
    public int longestPalindromeSubseq(String s) {
        int[][] dp=new int[s.length()][s.length()];
        for(int i=0;i<s.length();i++)
            dp[i][i]=1;
        char[] c=s.toCharArray();
        for(int i=s.length()-1;i>=0;i--)
            for(int j=i+1;j<s.length();j++){
                if(c[i]==c[j])
                    dp[i][j]=dp[i+1][j-1]+2;
                else
                    dp[i][j]=Math.max(dp[i+1][j],dp[i][j-1]);
            }
        return dp[0][s.length()-1];
    }
}
